import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;





public class getphoto1 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	
	private static final String PIC_DIR = "e:/aphotos"; 
	
	
	private static final int TIME_OUT = 10000
	
	;
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String static_url = "http://www.ppmsg.com/siwameitui/201302/";
		for(int i=20000;i<21000;i++){
			System.out.println(static_url+i+".html");
			go(static_url+i+".html");
		}
		
		
	}
	
	public static void go(String url) throws Exception{
		Connection conn= Jsoup.connect(url); 
		Document doc1 = conn.get();

		Elements jpgs = doc1.select("img");
	
		for(int i=0;i<jpgs.size();i++){
			Element element = jpgs.get(i);
			final String imgurl = element.attr("src");
			//System.out.println(imgurl);
			Thread.sleep(1000);
			new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						save(imgurl);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
			
			
		}
	}
	
	public static void save(String url) throws Exception{
		String fileName = url.substring(url.lastIndexOf("/")+1).replaceAll("\r","");
		URL aaa = new URL(url);
		String fileFolder = aaa.getPath().substring(0,aaa.getPath().lastIndexOf("/")+1);
		String tempfilePath = PIC_DIR  + fileFolder;
		String filePath = tempfilePath+fileName;
		File bbb = new File(tempfilePath);
		if (!bbb.exists()){
			bbb.mkdirs();
		}
		
		//System.out.println(filePath);
		BufferedOutputStream out = null;
		byte[] bit = getByte(url.trim());
		if(bit.length > 0 ){
			
			out = new BufferedOutputStream(new FileOutputStream(filePath));
			out.write(bit);
			out.flush();
			System.out.println("create file success! [" + filePath + "]");
			
		}
		
		
		
	}
	
	 
	private static void getRemoteFile(String strUrl, String fileName) throws IOException { 
		    URL url = new URL(strUrl); 
		    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
		    DataInputStream input = new DataInputStream(conn.getInputStream()); 
		    DataOutputStream output = new DataOutputStream(new FileOutputStream(fileName)); 
		    byte[] buffer = new byte[1024 * 8]; 
		    int count = 0; 
		    while ((count = input.read(buffer)) > 0) { 
		      output.write(buffer, 0, count); 
		    } 
		    output.close(); 
		    input.close(); 
		   
		  }
	
	
	public static byte[] getByte(String uri){
		HttpClient client = new DefaultHttpClient();
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,TIME_OUT);
		HttpGet get = new HttpGet(uri);
		get.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT);
		try {
			HttpResponse response = client.execute(get);
			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
				
				HttpEntity entity = response.getEntity();
				if(entity!=null){
					return EntityUtils.toByteArray(entity);
				}
			}
//			else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_FORBIDDEN) {
//				
//			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		finally{
			//client.getConnectionManager().shutdown();
		}
		return new byte[0];
	}

}
